tools: replace sprintf with snprintf where applicable
authorKeir Fraser <keir.fraser@citrix.com>
Thu, 12 Jun 2008 14:41:15 +0000 (15:41 +0100)
committerKeir Fraser <keir.fraser@citrix.com>
Thu, 12 Jun 2008 14:41:15 +0000 (15:41 +0100)
Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
tools/console/daemon/io.c
tools/fs-back/fs-ops.c
tools/fs-back/fs-xenbus.c
tools/libfsimage/zfs/fsys_zfs.c
tools/libxc/xc_dom_x86.c
tools/misc/nsplitd/nsplitd.c
tools/misc/xenperf.c
tools/vnet/vnet-module/varp_util.h
tools/vnet/vnet-module/vnet.c
tools/vtpm_manager/migration/vtpm_migratord_handler.c
tools/xenmon/xenbaked.c

index 66db0f89cbe9bf45d48b851c21f481ed0f52cdcc..1a3f243a36d544e05b225616f9bb5c001429eee8 100644 (file)
@@ -525,7 +525,8 @@ static int domain_create_ring(struct domain *dom)
        } else
                dom->use_consolepath = 0;
 
-       sprintf(path, "%s/type", dom->use_consolepath ? dom->conspath: dom->serialpath);
+       snprintf(path, sizeof(path), "%s/type",
+               dom->use_consolepath ? dom->conspath: dom->serialpath);
        type = xs_read(xs, XBT_NULL, path, NULL);
        if (type && strcmp(type, "xenconsoled") != 0) {
                free(type);
index 0be13234b08cd501e1aa3e2e143386436dd9776f..de6bcd6b53c6aeb85fb51af723c4d6c18b95d2d7 100644 (file)
@@ -55,7 +55,8 @@ void dispatch_file_open(struct mount *mount, struct fsif_request *req)
     printf("File open issued for %s\n", file_name); 
     assert(BUFFER_SIZE > 
            strlen(file_name) + strlen(mount->export->export_path) + 1); 
-    sprintf(full_path, "%s/%s", mount->export->export_path, file_name);
+    snprintf(full_path, sizeof(full_path), "%s/%s",
+           mount->export->export_path, file_name);
     assert(xc_gnttab_munmap(mount->gnth, file_name, 1) == 0);
     printf("Issuing open for %s\n", full_path);
     fd = open(full_path, O_RDWR);
@@ -311,7 +312,8 @@ void dispatch_remove(struct mount *mount, struct fsif_request *req)
     printf("File remove issued for %s\n", file_name); 
     assert(BUFFER_SIZE > 
            strlen(file_name) + strlen(mount->export->export_path) + 1); 
-    sprintf(full_path, "%s/%s", mount->export->export_path, file_name);
+    snprintf(full_path, sizeof(full_path), "%s/%s",
+           mount->export->export_path, file_name);
     assert(xc_gnttab_munmap(mount->gnth, file_name, 1) == 0);
     printf("Issuing remove for %s\n", full_path);
     ret = remove(full_path);
@@ -355,8 +357,10 @@ void dispatch_rename(struct mount *mount, struct fsif_request *req)
            strlen(old_file_name) + strlen(mount->export->export_path) + 1); 
     assert(BUFFER_SIZE > 
            strlen(new_file_name) + strlen(mount->export->export_path) + 1); 
-    sprintf(old_full_path, "%s/%s", mount->export->export_path, old_file_name);
-    sprintf(new_full_path, "%s/%s", mount->export->export_path, new_file_name);
+    snprintf(old_full_path, sizeof(old_full_path), "%s/%s",
+           mount->export->export_path, old_file_name);
+    snprintf(new_full_path, sizeof(new_full_path), "%s/%s",
+           mount->export->export_path, new_file_name);
     assert(xc_gnttab_munmap(mount->gnth, buf, 1) == 0);
     printf("Issuing rename for %s -> %s\n", old_full_path, new_full_path);
     ret = rename(old_full_path, new_full_path);
@@ -398,7 +402,8 @@ void dispatch_create(struct mount *mount, struct fsif_request *req)
     printf("File create issued for %s\n", file_name); 
     assert(BUFFER_SIZE > 
            strlen(file_name) + strlen(mount->export->export_path) + 1); 
-    sprintf(full_path, "%s/%s", mount->export->export_path, file_name);
+    snprintf(full_path, sizeof(full_path), "%s/%s",
+           mount->export->export_path, file_name);
     assert(xc_gnttab_munmap(mount->gnth, file_name, 1) == 0);
     /* We can advance the request consumer index, from here on, the request
      * should not be used (it may be overrinden by a response) */
@@ -447,7 +452,8 @@ void dispatch_list(struct mount *mount, struct fsif_request *req)
     printf("Dir list issued for %s\n", file_name); 
     assert(BUFFER_SIZE > 
            strlen(file_name) + strlen(mount->export->export_path) + 1); 
-    sprintf(full_path, "%s/%s", mount->export->export_path, file_name);
+    snprintf(full_path, sizeof(full_path), "%s/%s",
+           mount->export->export_path, file_name);
     /* We can advance the request consumer index, from here on, the request
      * should not be used (it may be overrinden by a response) */
     mount->ring.req_cons++;
@@ -540,7 +546,8 @@ void dispatch_fs_space(struct mount *mount, struct fsif_request *req)
     printf("Fs space issued for %s\n", file_name); 
     assert(BUFFER_SIZE > 
            strlen(file_name) + strlen(mount->export->export_path) + 1); 
-    sprintf(full_path, "%s/%s", mount->export->export_path, file_name);
+    snprintf(full_path, sizeof(full_path), "%s/%s",
+           mount->export->export_path, file_name);
     assert(xc_gnttab_munmap(mount->gnth, file_name, 1) == 0);
     printf("Issuing fs space for %s\n", full_path);
     ret = statfs(full_path, &stat);
index c0c7621fba8a78a1ae4c971ab0211bc64ca8ffe3..3a3009465f79ec3647e2aa73cd2eb3053be8bdcc 100644 (file)
@@ -22,8 +22,8 @@ static bool xenbus_printf(struct xs_handle *xsh,
     va_list args;
     
     va_start(args, fmt);
-    sprintf(fullpath,"%s/%s", node, path);
-    vsprintf(val, fmt, args);
+    snprintf(fullpath, sizeof(fullpath), "%s/%s", node, path);
+    vsnprintf(val, sizeof(val), fmt, args);
     va_end(args);
     printf("xenbus_printf (%s) <= %s.\n", fullpath, val);    
 
@@ -72,7 +72,7 @@ int xenbus_register_export(struct fs_export *export)
     printf("XS transaction is %d\n", xst); 
  
     /* Create node string */
-    sprintf(node, "%s/%d", EXPORTS_NODE, export->export_id); 
+    snprintf(node, sizeof(node), "%s/%d", EXPORTS_NODE, export->export_id); 
     /* Remove old export (if exists) */ 
     xs_rm(xsh, xst, node);
 
@@ -116,20 +116,20 @@ void xenbus_read_mount_request(struct mount *mount, char *frontend)
 
     assert(xsh != NULL);
 #if 0
-    sprintf(node, WATCH_NODE"/%d/%d/frontend", 
+    snprintf(node, sizeof(node), WATCH_NODE"/%d/%d/frontend", 
                            mount->dom_id, mount->export->export_id);
     frontend = xs_read(xsh, XBT_NULL, node, NULL);
 #endif
     mount->frontend = frontend;
-    sprintf(node, "%s/state", frontend);
+    snprintf(node, sizeof(node), "%s/state", frontend);
     s = xs_read(xsh, XBT_NULL, node, NULL);
     assert(strcmp(s, STATE_READY) == 0);
     free(s);
-    sprintf(node, "%s/ring-ref", frontend);
+    snprintf(node, sizeof(node), "%s/ring-ref", frontend);
     s = xs_read(xsh, XBT_NULL, node, NULL);
     mount->gref = atoi(s);
     free(s);
-    sprintf(node, "%s/event-channel", frontend);
+    snprintf(node, sizeof(node), "%s/event-channel", frontend);
     s = xs_read(xsh, XBT_NULL, node, NULL);
     mount->remote_evtchn = atoi(s);
     free(s);
@@ -158,12 +158,12 @@ void xenbus_write_backend_node(struct mount *mount)
     assert(xsh != NULL);
     self_id = get_self_id();
     printf("Our own dom_id=%d\n", self_id);
-    sprintf(node, "%s/backend", mount->frontend);
-    sprintf(backend_node, "/local/domain/%d/"ROOT_NODE"/%d",
+    snprintf(node, sizeof(node), "%s/backend", mount->frontend);
+    snprintf(backend_node, sizeof(backend_node), "/local/domain/%d/"ROOT_NODE"/%d",
                                 self_id, mount->mount_id);
     xs_write(xsh, XBT_NULL, node, backend_node, strlen(backend_node));
 
-    sprintf(node, ROOT_NODE"/%d/state", mount->mount_id);
+    snprintf(node, sizeof(node), ROOT_NODE"/%d/state", mount->mount_id);
     xs_write(xsh, XBT_NULL, node, STATE_INITIALISED, strlen(STATE_INITIALISED));
 }
 
@@ -174,7 +174,7 @@ void xenbus_write_backend_ready(struct mount *mount)
 
     assert(xsh != NULL);
     self_id = get_self_id();
-    sprintf(node, ROOT_NODE"/%d/state", mount->mount_id);
+    snprintf(node, sizeof(node), ROOT_NODE"/%d/state", mount->mount_id);
     xs_write(xsh, XBT_NULL, node, STATE_READY, strlen(STATE_READY));
 }
 
index f9f94d808a174cf6737c5f577b21c3b00ddb118c..d6f9880babe1a60508343719d3c9909f77eed9a3 100644 (file)
@@ -1336,7 +1336,7 @@ zfs_open(fsi_file_t *ffi, char *filename)
                        char zfs_bootstr[] = "zfs-bootfs=";
                        char zfs_bootpath[] = ",bootpath='";
 
-                       sprintf(temp, "%llu", (unsigned long long)
+                       snprintf(temp, sizeof(temp), "%llu", (unsigned long long)
                            current_bootfs_obj);
                        alloc_size = strlen(zfs_bootstr) +
                            strlen(current_rootpool) +
index 2ed298907c50485489620a35b727cd83464008b5..f96ec3f7ab1682973770b1379ad65757ee90fc35 100644 (file)
@@ -418,7 +418,7 @@ static int start_info_x86_32(struct xc_dom_image *dom)
     xc_dom_printf("%s: called\n", __FUNCTION__);
 
     memset(start_info, 0, sizeof(*start_info));
-    sprintf(start_info->magic, dom->guest_type);
+    snprintf(start_info->magic, sizeof(start_info->magic), dom->guest_type);
     start_info->nr_pages = dom->total_pages;
     start_info->shared_info = shinfo << PAGE_SHIFT_X86;
     start_info->pt_base = dom->pgtables_seg.vstart;
@@ -457,7 +457,7 @@ static int start_info_x86_64(struct xc_dom_image *dom)
     xc_dom_printf("%s: called\n", __FUNCTION__);
 
     memset(start_info, 0, sizeof(*start_info));
-    sprintf(start_info->magic, dom->guest_type);
+    snprintf(start_info->magic, sizeof(start_info->magic), dom->guest_type);
     start_info->nr_pages = dom->total_pages;
     start_info->shared_info = shinfo << PAGE_SHIFT_X86;
     start_info->pt_base = dom->pgtables_seg.vstart;
index 48fbd65103249490df17b5f2a66701ae26f337ba..32f0b5684584281fa8dfb0f6398772b15d104a9a 100644 (file)
@@ -106,7 +106,7 @@ static void fault(char *format, ...)
     
     /* XXX This is a bit dubious, but there is no vsyslog */
     va_start(ap, format);
-    vsprintf(logbuf, format, ap);
+    vsnprintf(logbuf, sizeof(logbuf), format, ap);
     syslog(LOG_ERR, logbuf);
     va_end(ap);
     exit(1);
index afa635a034000437591842e76f8c60c2e670e32c..e91c5b63620a7075b6d0633586d3237af746c954 100644 (file)
@@ -202,7 +202,7 @@ int main(int argc, char *argv[])
                         strncpy(hypercall_name, hypercall_name_table[j],
                                 sizeof(hypercall_name));
                     else
-                        sprintf(hypercall_name, "[%d]", j);
+                        snprintf(hypercall_name, sizeof(hypercall_name), "[%d]", j);
                     hypercall_name[sizeof(hypercall_name)-1]='\0';
                     printf("%-35s ", hypercall_name);
                     printf("%12u\n", (unsigned int)val[j]);
index d4610290887493899aea90aaadac72a4edd6e5fe..2f671b3b6bfe4a8aa5478522836cdcf8d9ce1d30 100644 (file)
@@ -53,11 +53,11 @@ static inline const char *VarpAddr_ntoa(VarpAddr *addr, char buf[VARP_ADDR_BUF])
     switch(addr->family){
     default:
     case AF_INET:
-        sprintf(buf, "%u.%u.%u.%u",
+        snprintf(buf, sizeof(buf), "%u.%u.%u.%u",
                 NIPQUAD(addr->u.ip4));
         break;
     case AF_INET6:
-        sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
+        snprintf(buf, sizeof(buf), "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
                 NIP6(addr->u.ip6));
         break;
     }
@@ -66,7 +66,7 @@ static inline const char *VarpAddr_ntoa(VarpAddr *addr, char buf[VARP_ADDR_BUF])
 
 static inline const char *VnetId_ntoa(VnetId *vnet, char buf[VNET_ID_BUF])
 {
-    sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
+    snprintf(buf, sizeof(buf), "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
             ntohs(vnet->u.vnet16[0]), \
             ntohs(vnet->u.vnet16[1]), \
             ntohs(vnet->u.vnet16[2]), \
index 51796415cc59a9e03f8af738cadc0e3006dde1d4..b213292a156fc9091e79bfdadea8b2bd01897e4e 100644 (file)
@@ -354,7 +354,7 @@ static int vnet_setup(void){
         if(err) break;
         vnetid = VNET_VIF + i;
         vnet->vnet = toVnetId(vnetid);
-        sprintf(vnet->device, "vnif%04x", vnetid);
+        snprintf(vnet->device, sizeof(vnet->device), "vnif%04x", vnetid);
         vnet->security = (vnetid > 10 ? security : 0);
         err = Vnet_create(vnet);
         Vnet_decref(vnet);
index 80c46f01b9528a9b60a8ae96b45fa330c93c7b53..0a8a2d52c1a0e9bee0f24045d9f2ec2903ee43d3 100644 (file)
@@ -49,8 +49,8 @@
 // This must be updated to the longest command name. Currently GETINST
 #define VTPM_SH_CMD_SIZE (strlen(VTPM_SH_CMD_HDR) + strlen(VTPM_SH_CMD_FTR) + 1 + strlen(VTPM_SH_GETINST) + 2)
 
-void handle_vtpm_mig_step2(buffer_t *in_param_buf, buffer_t *result_buf){
-
+void handle_vtpm_mig_step2(buffer_t *in_param_buf, buffer_t *result_buf)
+{
   TPM_TAG tag = VTPM_TAG_RSP;
   buffer_t out_param_buf= NULL_BUF, mig_key_buf=NULL_BUF; 
   TPM_RESULT status=TPM_SUCCESS, cmd_status;
@@ -97,13 +97,14 @@ void handle_vtpm_mig_step2(buffer_t *in_param_buf, buffer_t *result_buf){
   return;
 }
 
-void handle_vtpm_mig_step3(buffer_t *in_param_buf, buffer_t *result_buf){
-  
+void handle_vtpm_mig_step3(buffer_t *in_param_buf, buffer_t *result_buf)
+{
   TPM_TAG tag = VTPM_TAG_RSP;
   buffer_t out_param_buf= NULL_BUF, mig_key_buf=NULL_BUF, empty_buf=NULL_BUF;
   TPM_RESULT status=TPM_SUCCESS, cmd_status;
   UINT32 out_param_size, instance;
   char *shell_cmd_str=NULL;
+  size_t shell_cmd_strlen;
   FILE *shell_f=NULL;
 
   if ( (!in_param_buf) || (!result_buf) ) {
@@ -124,16 +125,20 @@ void handle_vtpm_mig_step3(buffer_t *in_param_buf, buffer_t *result_buf){
   }
 
   // ====== Call hotplug-script and get an instance ======
-  shell_cmd_str = (char *) malloc(VTPM_SH_CMD_SIZE + name_data32.size + 10); // 10 is just padding for the UINT32
+  shell_cmd_strlen = VTPM_SH_CMD_SIZE + name_data32.size + 10;
+  shell_cmd_str = (char *) malloc(shell_cmd_strlen); // 10 is just padding for the UINT32
 
-  sprintf(shell_cmd_str, VTPM_SH_CMD_HDR VTPM_SH_GETINST VTPM_SH_CMD_FTR);
+  snprintf(shell_cmd_str, shell_cmd_strlen,
+       VTPM_SH_CMD_HDR VTPM_SH_GETINST VTPM_SH_CMD_FTR);
 
   shell_f = popen(shell_cmd_str, "r");
   fscanf(shell_f, "%d", &instance);
   pclose(shell_f);
   
   // ====== Call hotplug-script and add instance ======
-  sprintf(shell_cmd_str, VTPM_SH_CMD_HDR VTPM_SH_ADD " %s %d" VTPM_SH_CMD_FTR, name_data32.data, instance);
+  snprintf(shell_cmd_str, shell_cmd_strlen,
+       VTPM_SH_CMD_HDR VTPM_SH_ADD " %s %d" VTPM_SH_CMD_FTR,
+       name_data32.data, instance);
   system(shell_cmd_str);
 
   // ========= Call vtpm_manager and load VTPM =======
@@ -156,7 +161,8 @@ void handle_vtpm_mig_step3(buffer_t *in_param_buf, buffer_t *result_buf){
   TPMTRYRETURN(cmd_status);
 
   // ====== Call hotplug-script and resume instance ======
-  sprintf(shell_cmd_str, VTPM_SH_CMD_HDR VTPM_SH_RESUME " %d" VTPM_SH_CMD_FTR, instance);
+  snprintf(shell_cmd_str, shell_cmd_strlen,
+       VTPM_SH_CMD_HDR VTPM_SH_RESUME " %d" VTPM_SH_CMD_FTR, instance);
   system(shell_cmd_str);
 
   goto egress;
index 35ba8e40e987eeb1c8a1fcfb879f3239c09ea6c6..4adc69ebc422d66c242b40aab7c3ba7b24890077 100644 (file)
@@ -749,9 +749,13 @@ void qos_init_domain(int domid, int idx)
     new_qos->domain_info[idx].blocked_start_time = 0;
     new_qos->domain_info[idx].id = domid;
     if (domid == IDLE_DOMAIN_ID)
-        sprintf(new_qos->domain_info[idx].name, "Idle Task%d", global_cpu);
+        snprintf(new_qos->domain_info[idx].name,
+               sizeof(new_qos->domain_info[idx].name),
+               "Idle Task%d", global_cpu);
     else
-        sprintf(new_qos->domain_info[idx].name, "Domain#%d", domid);
+        snprintf(new_qos->domain_info[idx].name,
+               sizeof(new_qos->domain_info[idx].name),
+               "Domain#%d", domid);
   
     for (i=0; i<NSAMPLES; i++) {
         new_qos->qdata[i].ns_gotten[idx] = 0;